package org.example.elm_springboot.controller;

import org.example.elm_springboot.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(name = "user-service")
public interface UserServiceClient {
    @GetMapping("/user/{userId}")
    User getUserById(@PathVariable("userId") String userId);
}

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private UserServiceClient userServiceClient;

    @PostMapping("/add")
    public void addItem(@RequestParam String userId, @RequestParam String itemId, @RequestParam int quantity) {
        // 调用用户服务获取用户信息
        User user = userServiceClient.getUserById(userId);
        cartService.addItemToCart(userId, itemId, quantity);
    }

    @GetMapping("/view")
    public Map<String, Integer> viewCart(@RequestParam String userId) {
        return cartService.getCart(userId);
    }

    @DeleteMapping("/remove")
    public void removeItem(@RequestParam String userId, @RequestParam String itemId) {
        cartService.removeItemFromCart(userId, itemId);
    }
} 